package com.lib.utls.appupdate;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.base.R;
import com.lib.bean.UpdateVersionBean;
import com.lib.utls.glide.GlideConfig;
import com.luck.picture.lib.tools.ToastManage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;

public class AppUpdateDialog extends AlertDialog implements View.OnClickListener {

    private UpdateVersionBean mAppUpdateInfo;
    private OnUpdateClickListener mOnUpdateListener;
    private ProgressBar mProgressBar;
    private TextView mValueText;

    private Context context;

    Button btnUpdate;

    public AppUpdateDialog(Context context) {
        super(context);
        this.context = context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_app_update);

        initView();
    }

    public void setAppUpdateInfo(UpdateVersionBean mAppUpdateInfo) {
        this.mAppUpdateInfo = mAppUpdateInfo;
    }

    public void setProgress(int progress, int maxValue) {
        if (maxValue == 0) {
            return;
        }
        if (progress > 0 && progress < maxValue) {
            mProgressBar.setMax(maxValue);
            mProgressBar.setProgress(progress);
            mValueText.setText((int) (progress * 1.0f / maxValue * 100) + "%");
        } else if (progress == maxValue) {
            mProgressBar.setVisibility(View.GONE);
            mValueText.setVisibility(View.GONE);
            btnUpdate.setText("立即安装");
        }
    }

    public void setOnUpdateClickListener(OnUpdateClickListener mOnUpdateListener) {
        this.mOnUpdateListener = mOnUpdateListener;
    }

    protected void initView() {
        setCanceledOnTouchOutside(false);

        ImageView imageIcon = findViewById(R.id.iv_update_icon);
        ImageView imageCancel = findViewById(R.id.btn_update_cancel);

        TextView tvVersion = findViewById(R.id.tv_versioncode);
        TextView tvDes1 = findViewById(R.id.tv_versiontext1);
        TextView tvDes2 = findViewById(R.id.tv_versiontext2);
        TextView tvDes3 = findViewById(R.id.tv_versiontext3);
        btnUpdate = findViewById(R.id.btn_update_sure);

        mProgressBar = findViewById(R.id.progressbarvalue);
        mValueText = findViewById(R.id.tv_currentVal);

        btnUpdate.setOnClickListener(this);
        imageCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUpdateDialog.this.dismiss();
            }
        });

        Glide.with(context)
                .load(mAppUpdateInfo.getObj().getIcon())
                .apply(GlideConfig.getRoundOptions(1))
                .into(imageIcon);

        tvVersion.setText("发现新版本" + mAppUpdateInfo.getObj().getVersion_name());

        List<String> desList = new ArrayList<>();
        desList = mAppUpdateInfo.getObj().getVersion_introduction();


        if (desList.size() == 1) {

            tvDes1.setText(desList.get(0));
            tvDes2.setVisibility(View.INVISIBLE);
            tvDes3.setVisibility(View.INVISIBLE);

        } else if (desList.size() == 2) {

            tvDes1.setText(desList.get(0));
            tvDes2.setText(desList.get(1));
            tvDes3.setVisibility(View.INVISIBLE);

        } else if (desList.size() == 3) {

            tvDes1.setText(desList.get(0));
            tvDes2.setText(desList.get(1));
            tvDes3.setText(desList.get(2));

        }

    }

    protected int getStartColor() {
        return getContext().getResources().getColor(R.color.colorPrimary);
    }

    protected int getEndColor() {
        return getContext().getResources().getColor(R.color.colorAccent);
    }

    @Override
    public void onClick(View v) {
        // update
        if (mOnUpdateListener != null) {
//            findViewById(R.id.progressbarvalue).setVisibility(View.VISIBLE);
//            findViewById(R.id.tv_currentVal).setVisibility(View.VISIBLE);
//            btnUpdate.setText("学思兔" + mAppUpdateInfo.getObj().getVersion_name() + "正在下载中.....");
            ToastManage.s(context, "学思兔" + mAppUpdateInfo.getObj().getVersion_name() + "正在下载中...");
            AppUpdateDialog.this.dismiss();

            mOnUpdateListener.update(AppUpdateDialog.this);

        }
    }

    public interface OnUpdateClickListener {

        void update(AppUpdateDialog dialog);

    }

}
